package studentdemo.dao;

import studentdemo.modul.Report;
import studentdemo.util.DBUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ReportDaoImpl implements ReportDao {
    @Override
    public int insertReport(Report report) {
        String sql = "INSERT INTO Dujx_Report11 (djx_Resno11, djx_Recno11, djx_semester11, djx_grade11, djx_Retno11) VALUES (?, ?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, report.getResno());
            ps.setString(2, report.getRecno());
            ps.setString(3, report.getSemester());
            ps.setFloat(4, report.getGrade());
            ps.setString(5, report.getRetno());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int updateReport(Report report) {
        String sql = "UPDATE Dujx_Report11 SET djx_semester11=?, djx_grade11=?, djx_Retno11=? WHERE djx_Resno11=? AND djx_Recno11=?";
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, report.getSemester());
            ps.setFloat(2, report.getGrade());
            ps.setString(3, report.getRetno());
            ps.setString(4, report.getResno());
            ps.setString(5, report.getRecno());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int deleteReport(String resno, String recno) {
        String sql = "DELETE FROM Dujx_Report11 WHERE djx_Resno11=? AND djx_Recno11=?";
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, resno);
            ps.setString(2, recno);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public List<Report> getReportsByStudent(String resno) {
        String sql = "SELECT * FROM Dujx_Report11 WHERE djx_Resno11=?";
        List<Report> list = new ArrayList<>();
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, resno);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                list.add(extractReport(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Report> getReportsByCourse(String recno) {
        String sql = "SELECT * FROM Dujx_Report11 WHERE djx_Recno11=?";
        List<Report> list = new ArrayList<>();
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, recno);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                list.add(extractReport(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Report> getReportsByTeacher(String retno) {
        String sql = "SELECT * FROM Dujx_Report11 WHERE djx_Retno11=?";
        List<Report> list = new ArrayList<>();
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, retno);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                list.add(extractReport(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Report> getReportsBySemester(String semester) {
        String sql = "SELECT * FROM Dujx_Report11 WHERE djx_semester11=?";
        List<Report> list = new ArrayList<>();
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, semester);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                list.add(extractReport(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Report> getAllReports() {
        String sql = "SELECT * FROM Dujx_Report11";
        List<Report> list = new ArrayList<>();
        try (Connection conn = DBUtil.getConnection(); Statement st = conn.createStatement()) {
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                list.add(extractReport(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    private Report extractReport(ResultSet rs) throws SQLException {
        return new Report(
                rs.getString("djx_Resno11"),
                rs.getString("djx_Recno11"),
                rs.getString("djx_semester11"),
                rs.getFloat("djx_grade11"),
                rs.getString("djx_Retno11")
        );
    }
} 